home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / inn1.000 / inn1.4sec-linux-src.tar / inn / samples / writelog < prev   
Text File  |  1993-12-22  |  1KB  |  58 lines

  1. #! /bin/sh
  2. ##  $Revision: 1.3 $
  3. ##  Write a log file entry, by either mailing it or writing it safely.
  4. ##  Usage:
  5. ##    writelog name text... <input
  6. ##  where
  7. ##    name    is 'mail' to mail it, or filename to append to.
  8.  
  9. ##  =()<. @<_PATH_SHELLVARS>@>()=
  10. . /news/lib/innshellvars
  11.  
  12. MAXTRY=60
  13.  
  14. ##  Parse arguments.
  15. if [ $# -lt 2 ] ; then
  16.     echo "usage: $0 'logfile|mail' message ..." 1>&2
  17.     exit 1
  18. fi
  19. LOGFILE="$1"
  20. shift
  21. MESSAGE="$@"
  22.  
  23. ##  Handle the easy cases.
  24. case "X${LOGFILE}" in
  25. X/dev/null)
  26.     exit 0
  27.     ;;
  28. Xmail)
  29.     sed -e 's/^~/~~/' | ${MAILCMD} -s "${MESSAGE}" ${NEWSMASTER}
  30.     exit 0
  31.     ;;
  32. esac
  33.  
  34. ##  We're sending to a file.
  35. LOCK=${LOCKS}/LOCK.`basename ${LOGFILE}`
  36.  
  37. ##  Remember our PID, in case while is a sub-shell.
  38. PID=$$
  39. TRY=0
  40.  
  41. export LOCK MAXTRY PID LOGFILE ARTICLE MESSAGE TRY
  42. while [ ${TRY} -lt ${MAXTRY} ]; do
  43.     shlock -p ${PID} -f ${LOCK} && break
  44.     sleep 2
  45.     TRY=`expr ${TRY} + 1`
  46. done
  47.  
  48. ##  If we got the lock, update the file; otherwise, give up.
  49. if [ ${TRY} -lt ${MAXTRY} ]; then
  50.     echo "${MESSAGE}" >>${LOGFILE}
  51.     ${SED} -e 's/^/    /' >>${LOGFILE}
  52.     echo "" >>${LOGFILE}
  53.     rm -f ${LOCK}
  54. else
  55.     ##  This goes to errlog, usually.
  56.     echo "$0: Cannot grab lock ${LOCK}, held by:" `cat ${LOCK}` 1>&2
  57. fi
  58.